﻿using System;

namespace ConsoleGame
{
    static class Program
    {
        const string TextMenu = 
        "Main Menu:\n1 - Plant potato.\n2 - Plant flower.\n3 - Get current info.\n4 - Exit.";

        static string TextInfo = "";

        static string UserAnswer = "";

        static void Main()
        {
            Core.Main.Start();
            WriteMenu();
        }

        static void WriteMenu()
        {
            while (UserAnswer != "0")
            {
                Console.Clear();
                Console.WriteLine(TextMenu);
                switch (UserAnswer)
                {
                    case "1":
                        PutPlant(Core.PlantType.Potato);
                        break;
                    case "2":
                        PutPlant(Core.PlantType.Flower);
                        break;
                    case "3":
                        GetInfo();
                        break;
                    case "4":
                        Environment.Exit(0);
                        break;
                }

                Console.WriteLine(TextInfo);
                UserAnswer = Console.ReadKey(true).KeyChar.ToString();
            }
        }

        static void GetInfo()
        {
            var str = "";
            str += string.Format("Your money: {0}\n", Core.Main.Money);
            foreach (var field in Core.Main.Plot)
            {
                if(!field.Empty)
                {
                    var timeToGrowup = field.Plant.TimeToGrowup;
                    var tt = field.Plant.PlantingDate.AddMinutes(timeToGrowup) - DateTime.Now;
                    
                    str += string.Format(
                        "On the field number {0} growing {1},  to harvest {2} seconds.\n",
                        field.ID + 1,
                        field.Plant.Name,
                        Math.Round(tt.TotalSeconds));
                }
                else
                {
                    str += string.Format("The field number {0} is empty.\n", field.ID);
                }
            }
            
            TextInfo = str;
        }

        static void PutPlant(Core.PlantType type)
        {
            var str = "";
            int fieldID;
            
            while (!int.TryParse(str, out fieldID) || 
                Core.Main.Plot.Count > fieldID && 
                fieldID <= 0)
            {
                Console.WriteLine("Please enter field number for new plant:");
                str = Console.ReadLine();
            }

            if(Core.Main.PutPlant(fieldID - 1, type) == 1)
            {
                TextInfo = "You do not have enough money\n";
            }

            UserAnswer = "";
            WriteMenu();
        }
    }
}
